<?php
/**
 * @author lan7
 * @since 2010-09-27
 */
if(!defined('IN_ORA')) {
	exit('Access Denied');
}
$GLOBALS['__msg_tpl__'] = "admin/msg.html";
session_start();
if(!isset($_SESSION["admin"])){
	exit('Access Denied');
}
$act = $GLOBALS['act'];
$GLOBALS['tpl']->assign('act',$act);

////////////////
if($act == 'nav'){
	$do = empty($GLOBALS['_param']['do'])?'list':$GLOBALS['_param']['do'];
	$GLOBALS['tpl']->assign('do',$do);
	//导航列表
	if($do == 'list'){
		$GLOBALS['tpl']->assign('nav',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort desc'));
	}
	//添加导航
	else if($do == 'add'){
		$GLOBALS['tpl']->assign('nav',$GLOBALS['db']->getAll('select * from '.tn('pages').' where is_nav = 1 order by Id desc'));
	}
	//删除导航
	else if($do == 'del'){
		$id = $GLOBALS['_param']['id'];
		if(is_numeric($id)){
			$GLOBALS['db']->execute("delete from ".tn('nav')." where Id = $id");
		}
		showMsg('操作成功!',parse_uri('admin_other-nav'),1);
	}
	//新增导航
	else if($do == 'save'){
		$nav_title = $_POST['nav_title'];
		$nav_url   = $_POST['nav_url'];
		$nav_target= $_POST['nav_target']?1:0;
		if(empty($nav_title)|$nav_title==''){
			showMsg('请输入导航名称!',$_SERVER['HTTP_REFERER'],1);
		}
		if(empty($nav_url)|$nav_url == ''){
			showMsg('请输入导航地址!',$_SERVER['HTTP_REFERER'],1);
		}
		$sql = "insert into ".tn("nav")."(nav_title,nav_url,nav_sort,nav_target) values('$nav_title','$nav_url',0,$nav_target)";
		if($GLOBALS['db']->execute($sql)){
			showMsg('操作成功!',parse_uri('admin_other-nav'),1);
		}else{
			showMsg('操作失败了!',parse_uri('admin_other-nav'),1);
		}
	}
	//输出模板页
	$GLOBALS['tpl']->display("admin/nav.html");
}
//////AJAX///////
else if($act == 'ajax'){
	$GLOBALS['is_ajax'] = true;
	$do = $GLOBALS['_param']['do'];
	if($do == 'navsort'){
		$id = $GLOBALS['_param']['id'];
		$val= $GLOBALS['_param']['val'];
		if(is_numeric($id)){
			$id = intval($id);
			$val = intval($val);
			$sql = 'update '.tn("nav").' set nav_sort = '.$val.' where id = '.$id;
			$GLOBALS['db']->execute($sql);
			showMsg('250');
		}
	}
	else if($do == 'catsort'){
		$id = $GLOBALS['_param']['id'];
		$val= $GLOBALS['_param']['val'];
		if(is_numeric($id)){
			$id = intval($id);
			$val = intval($val);
			$sql = 'update '.tn("cat").' set sort = '.$val.' where id = '.$id;
			$GLOBALS['db']->execute($sql);
			showMsg('250');
		}
	}
}
/** Tags **/
else if($act == 'tags'){
	$do = empty($GLOBALS['_param']['do'])?'list':$GLOBALS['_param']['do'];
	$GLOBALS['tpl']->assign('do',$do);
	if($do == 'check'){
		$sql = "select * from ".tn('tags')."  group by tag_name order by Id desc ";
		$GLOBALS['tpl']->assign('tags',$GLOBALS['db']->getAll($sql));
		$GLOBALS['tpl']->assign('page_title','标签列表');
	}
	$tags = array();
	$tags["art"] = array();
	$tags["pro"] = array();
	$tagNames = $GLOBALS['db']->getAll("select tag_name from ".tn("tags")." group by tag_name order by Id desc ");
	foreach($tagNames as $v){
		$sql = "select * from ".tn("tags")." where tag_name = '{$v['tag_name']}'";
		$tmp = $GLOBALS['db']->getAll($sql);
		for ($i=0;$i<sizeof($tmp);$i++){
			if($tmp[$i]["tag_type"] == 1){//Pro
				$pro = $GLOBALS['db']->getOne("select pro_name from ".tn("product")." where Id = ".$tmp[$i]["fk_id"]);
				$tmp[$i]["title"] = $pro["pro_name"];
				$tags["pro"][$tmp[$i]["Id"]]  = $tmp[$i];
			}else{
				$art = $GLOBALS['db']->getOne("select title from ".tn("article")." where Id = ".$tmp[$i]["fk_id"]);
				$tmp[$i]["title"] = $art["title"];
				$tags["art"][$tmp[$i]["Id"]]  = $tmp[$i];
			}
		}
	}
	$GLOBALS['tpl']->assign("full_tags",$tags);
	$GLOBALS['tpl']->display('admin/tags.html');
}
/** Case Template **/
else if($act == 'ptpl'){
	$do = isset($GLOBALS['_param']['do'])?$GLOBALS['_param']['do']:'list';
	$GLOBALS['tpl']->assign("do",$do);
	if($do == 'list'){
		$GLOBALS['tpl']->assign("pts",$GLOBALS['db']->getAll("select * from ".tn("ptpl")." order by Id desc"));
	}
	else if($do == 'modify' || $do == 'add'){
		$id = $GLOBALS['_param']['id'];
		if(is_numeric($id)){
			$ptpl = $GLOBALS['db']->getOne("select * from ".tn("ptpl")."  where Id = $id ");
			if($ptpl){
				$ptpl['tpl'] = unserialize($ptpl['tpl']);
				$GLOBALS['tpl']->assign("ptpl",$ptpl);
			}else{
				showMsg("无此记录!");
			}
		}
	}
	else if($do == 'save'){
		$attr = $_POST["attr"];
		$attr = @array_filter($attr,"loseEmpty");
		if(empty($attr)){
			showMsg("请添加属性!",$_SERVER["HTTP_REFERER"],1);
		}
		$tplstr = serialize($attr);
		$cname = clear_html($_POST["cname"]);
		if($cname==""){
			showMsg("请输入模板名称!",$_SERVER["HTTP_REFERER"],1);
		}
		$id = $_POST['Id'];
		if(is_numeric($id)&&$id>0){
			$sql = "update ".tn("ptpl")." set cname = '$cname' , tpl = '$tplstr' where Id = $id";
		}else{
			$sql = "insert into ".tn("ptpl")." values(default,'$cname','$tplstr')";
		}
		$GLOBALS['db']->execute($sql);
		showMsg("操作成功!",parse_uri("admin_other-ptpl"),1);
	}
	$GLOBALS['tpl']->display("admin/ptpl.html");

}else if ($act == 'info') {
	if(isset($_POST['submit'])){
		$inf_id 	=	$_POST['inf_id'];
		$inf_name 	= 	clear_html($_POST['inf_name']);
		$inf_qq		=	clear_html($_POST['inf_qq']);
		$inf_mail	=	clear_html($_POST['inf_mail']);
		$inf_zip	=	clear_html($_POST['inf_zip']);
		$inf_phone	=	clear_html($_POST['inf_phone']);
		$inf_cellphone=	clear_html($_POST['inf_cellphone']);
		$inf_address=	clear_html($_POST['inf_address']);
		$inf_icp	=	clear_html($_POST['inf_icp']);
		$inf_copy	=	clear_html($_POST['inf_copy']);
		if(intval($inf_id)>0){
			$sql = "update ".tn('info')." set inf_name='$inf_name',inf_qq='$inf_qq',inf_mail='$inf_mail',inf_zip='$inf_zip'";
			$sql.= ",inf_phone='$inf_phone',inf_cellphone='$inf_cellphone',inf_address='$inf_address',inf_icp='$inf_icp',";
			$sql.=" inf_copy='$inf_copy' where Id = $inf_id";
		}else{
			$sql = "insert into ".tn('info')." values(default,'$inf_name','$inf_qq','$inf_mail','$inf_zip','$inf_phone','$inf_cellphone','$inf_address','$inf_icp','$inf_copy')";
		}
		if($GLOBALS['db']->execute($sql)){
			showMsg("更新成功!",parse_uri('admin_other-info'),true);
		}
	}
	$GLOBALS['tpl']->assign('info',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));
	$GLOBALS['tpl']->display('admin/info.html');
}
function loseEmpty($var){
	return !(empty($var)&&$var == "");
}
?>